import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate';
import { uuid } from 'vue-uuid';
const store = createStore({
    state: {
        // 公共数据
        cartList: [],
        addressList: [
            {
                id: '1',
                name: '张三',
                tel: '13000000000',
                address: '浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室',
                isDefault: true,
                province: '浙江省',
                city: '杭州市',
                county: '西湖区',
                addressDetail: '文三路 138 号东方通信大厦 7 楼 501 室',
            },
            {
                id: '2',
                name: '李四',
                tel: '13100000000',
                address: '浙江省杭州市拱墅区莫干山路 50 号',
                province: '浙江省',
                city: '杭州市',
                county: '拱墅区',
                addressDetail: '莫干山路 50 号',
            },
        ],
    },
    getters: {
        totalCount(state) {
            let result = 0;
            state.cartList.forEach(item => {
                result += item.count
            })
            return result
        },
        totalPrice(state) {
            let result = 0;
            state.cartList.forEach(item => {
                if (item.checked) {
                    result += item.count * item.price
                }
            })
            return result * 100
        },
        orderList(state) {
            return state.cartList.filter(item => item.checked)
        },
        defaultAddress(state) {
            return state.addressList.find(item => item.isDefault)
        }
    },
    mutations: {
        addItem(state, payload) {
            const index = state.cartList.findIndex(item => item.id === payload.id)
            if (index > -1) {
                state.cartList[index].count++;
            } else {
                state.cartList.push({
                    ...payload,
                    count: 1,
                    checked: false,
                })
            }
        },
        changeCheck(state, payload) {
            // payload的值： 当全选框被勾上，true.  当全选框被取消勾选，false
            state.cartList.forEach(item => item.checked = payload)
        },
        deleteCart(state, payload) {
            state.cartList = state.cartList.filter(item => item.id !== payload)
        },
        addAddress(state, payload) {
            state.addressList.push({ ...payload, id: uuid.v1() })
        },
        editAddress(state, payload) {
            state.addressList.forEach(item => {
                if (item.id == payload.id) {
                    item.name = payload.name
                    item.tel = payload.tel
                    item.address = payload.address
                    item.province = payload.province
                    item.city = payload.city
                    item.county = payload.county
                    item.addressDetail = payload.addressDetail
                    // item= { ...payload }
                }
            })
        },
        changeisDefault(state, payload) {
            // 把所有的item 的isDefault值设为false
            state.addressList.forEach(item => {
                item.isDefault = false
                // id对应的这个地址的isDefault设置为true
                if (item.id === payload) {
                    item.isDefault = true
                }
            })

        }
    },
    plugins: [
        createPersistedState({
            storage: window.sessionStorage, // 或者 localStorage
        })
    ]
})

export default store;